package DOM;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomDemo {

	public static void main(String[] args) {
		DomDemo d = new DomDemo();
		try {
			d.readXML();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void readXMLTest() throws ParserConfigurationException, SAXException,
	IOException {
		// 1.获取工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.产生解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 3.解析xml文档，得到代表文档的document
		Document document = builder.parse(new File("C:\\A16TEST003.plmxml"));
		NodeList list = document.getElementsByTagName("ProductRevisionView");
		
		for (int i = 0; i < list.getLength(); i++) {
			Node node = list.item(i);
			System.out.println(node.getNodeName());
		//	getAllNode(node);
		}
		
	}
	private void readXML() throws ParserConfigurationException, SAXException,
			IOException {
		// 1.获取工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.产生解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 3.解析xml文档，得到代表文档的document
		Document document = builder.parse(new File("src/book1.xml"));
		NodeList list = document.getElementsByTagName("售价");

		for (int i = 0; i < list.getLength(); i++) {
			Node price = list.item(i);
			String money = price.getNodeName();
			System.out.println(money);
		}

	}

	private void addXML() throws ParserConfigurationException, SAXException,
			IOException, TransformerException {
		// 1.获取工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.产生解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 3.解析xml文档，得到代表文档的document
		Document document = builder.parse(new File("src/book1.xml"));
		Node price = document.createElement("售价");
		price.setTextContent("599");
		Node parent = document.getElementsByTagName("书").item(1);
		parent.appendChild(price);
		TransformerFactory f = TransformerFactory.newInstance();
		Transformer tf = f.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new File(
				"src/book1.xml")));
	}
	private void addXML2() throws ParserConfigurationException, SAXException,
	IOException, TransformerException {
		// 1.获取工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.产生解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 3.解析xml文档，得到代表文档的document
		Document document = builder.parse(new File("src/book1.xml"));
		Node node= document.createElement("售价");
		node.setTextContent("70.2");
		Node parent = document.getElementsByTagName("书").item(0);
		parent.insertBefore(node, document.getElementsByTagName("书名").item(0));
		
		TransformerFactory f = TransformerFactory.newInstance();
		Transformer tf = f.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new File(
		"src/book1.xml")));
	}
	public void deleteXML() throws ParserConfigurationException, SAXException,
	IOException, TransformerException {
		// 1.获取工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.产生解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 3.解析xml文档，得到代表文档的document
		Document document = builder.parse(new File("src/book1.xml"));
		 
		Node node = document.getElementsByTagName("售价").item(0);
		node.getParentNode().removeChild(node);
		
		TransformerFactory f = TransformerFactory.newInstance();
		Transformer tf = f.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new File(
		"src/book1.xml")));
	}
	public void updateAttribute() throws ParserConfigurationException, SAXException,
	IOException, TransformerException {
		// 1.获取工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.产生解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 3.解析xml文档，得到代表文档的document
		Document document = builder.parse(new File("src/book1.xml"));
		
		Element book=null;
		Node node=	document.getElementsByTagName("书").item(0);
		if(node.getNodeType()==Node.ELEMENT_NODE){//在做节点转换之前先判断，这样安全。
			book =(Element)node;
		}
		book.setAttribute("name", "miaomiao");//设置属性
		// book.removeAttribute("name");//删除属性
		
		TransformerFactory f = TransformerFactory.newInstance();
		Transformer tf = f.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new File(
		"src/book1.xml")));
	}

	private void updateXML() throws ParserConfigurationException, SAXException,
			IOException, TransformerException {
		// 1.获取工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.产生解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 3.解析xml文档，得到代表文档的document
		Document document = builder.parse(new File("src/book1.xml"));
		NodeList list = document.getElementsByTagName("售价");
		for (int i = 0; i < list.getLength(); i++) {
			Node price = list.item(i);
			price.setTextContent("99.99");
		}
		TransformerFactory f = TransformerFactory.newInstance();
		Transformer tf = f.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new File(
				"src/book1.xml")));
	}

	private void parseXML() throws ParserConfigurationException, SAXException,
			IOException {
		// 1.获取工厂
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// 2.产生解析器
		DocumentBuilder builder = factory.newDocumentBuilder();
		// 3.解析xml文档，得到代表文档的document
		Document document = builder.parse(new File("src/book1.xml"));
		// 遍历
		getAllNode(document);
	}

	private void getAllNode(Node node) {

		if (node.getNodeType() == Node.ELEMENT_NODE) {
			// System.out.println(node.getNodeName());
		 
			System.out.println(node.getNodeName());
		}

		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			Node child = list.item(i);
			getAllNode(child);
		}
	}
}
